package cn.kgc.ssm.interceptors;

import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 课工场
 * @date 2024/3/11
 * @description  登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 判定用户是否登录
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if (ObjectUtils.isEmpty(user)){
            // 重定向页面到登录页
            response.sendRedirect("/sport/admin/login.html");
            return  false;
        }

        return true;
    }
}
